package sch.jl.caigou.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jiliang.purchase.demo.CriteriaYongHulieBiao;
import jiliang.purchase.demo.YongHuliebiao;
import sch.jl.caigou.dao.YongHuliebiaoDao;
import sch.jl.caigou.utils.Page;

public class YongHuliebiaoImpl extends DaoImpl<YongHuliebiao> implements YongHuliebiaoDao
{
	
	private Map<String,String> map = new HashMap<String,String>();
	@Override
	public Page<YongHuliebiao> getYongHu(CriteriaYongHulieBiao CYH) {
		// TODO Auto-generated method stub
		init();
		Page<YongHuliebiao> page = new Page<YongHuliebiao>();
		List<YongHuliebiao> list = new ArrayList<YongHuliebiao>();
		list = getAllList(CYH);
		
		page.setPageNo(CYH.getPageNum());
		page.setPageSize(14);
		
		List<YongHuliebiao> pageList = new ArrayList<YongHuliebiao>();
		pageList = getPageList(list,CYH.getPageNum());
		for(int i=0;i<pageList.size();i++)
		{
			String js_id = changeJs_id(pageList.get(i).getJs_id());
			pageList.get(i).setJs_id(js_id);
		}
		
		page.setTotalItemNumber(list.size());
		page.setList(pageList);
		
		return page;
	}
	
	private String changeJs_id(String js_id)
	{
		String ji = ",";
		String[] jis = js_id.split("\\,");
		for(int i=1;i<jis.length;i++)
		{
			ji+=map.get(jis[i])+",";
		}
		return ji;
	}
	private void init()
	{
		map.put("1","申请人");
		map.put("2","分院审核");
		map.put("3","实验处审核");
		map.put("4","分院危化品打印");
		map.put("5","实验处气瓶打印");
		map.put("6","学校危化品仓库出库");
		map.put("7","分院危化品仓库入库");
		map.put("8","项目负责人审核");
		map.put("9","保卫处审核");
		map.put("10","采购中心审核及打印");
		map.put("20","超级管理员");
		map.put("31","分院气瓶管理");
		map.put("32","分院危化品管理");
		map.put("41","实验处气瓶管理");
		map.put("42","实验处危化品管理");
		map.put("43","采购中心管理");
		map.put("14","实验处危化品打印");
	}
	public List<YongHuliebiao> getAllList(CriteriaYongHulieBiao CYH)
	{
		List<YongHuliebiao> list = new ArrayList<YongHuliebiao>();
		String sql = "select gonghao,xingming,phone,js_id from think_yonghu where ";
		if(CYH.getDw_id()== null && CYH.getJs_id() == null && CYH.getXingming() == null)
		{
			sql = "select gonghao,xingming,phone,js_id from think_yonghu";
		}
		else
		{
			if(CYH.getDw_id()!= null)
			{
				sql+=" dw_id = '"+CYH.getDw_id()+"' and ";
			}
			if(CYH.getJs_id() !=null)
			{
				sql+="js_id like'%,"+CYH.getJs_id()+",%' and ";
			}
			if(CYH.getXingming() !=null)
			{
				sql+= "xingming = '"+CYH.getXingming()+"'";
			}
		}
		//System.out.println(sql);
		if (sql.substring(sql.length() - 3, sql.length()).equals("and"))
		{
			sql = sql.substring(0, sql.length() - 3);
		}
		
		//System.out.println(sql);
		list = queryForList(sql);
		return list;
	}
	
	private List<YongHuliebiao> getPageList(List<YongHuliebiao> list,int page)
	{
		List<YongHuliebiao> Pagelist = new ArrayList<YongHuliebiao>();
		if (list.size() == 0)
		{
			return Pagelist;
		}
		if (page * 10 >= list.size())
		{
			for (int i = (page - 1) * 10; i < list.size(); i++)
			{
				Pagelist.add(list.get(i));
			}
		} else
		{
			for (int i = 0; i < 10; i++)
			{
				Pagelist.add(list.get((page - 1)*10 + i));
			}
		}
		return Pagelist;
	}
	
	

}
